package com.innjia.app.controller;

import com.innjia.base.common.Constant;
import com.innjia.base.utils.PageUtils;
import com.innjia.base.utils.Result;
import com.innjia.sys.entity.WishListEntity;
import com.innjia.sys.service.WishListService;
import com.innjia.utils.RedisUtil;
import com.xiaoleilu.hutool.lang.Validator;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;


/**
 * 
 * 
 * @author innjia
 * @email innjia@innjia.com
 * @date 2017-11-30 17:44:47
 */
@RestController
@RequestMapping("app/wishlist")
public class AppWishListController extends BaseController{

    private Logger logger = Logger.getLogger(AppWishListController.class);
    @Autowired
	private WishListService wishListService;

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;
	

	/**
	 * 保存新建的心愿单
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	//@RequiresPermissions("wishlist:save")
	public Result save(@RequestBody WishListEntity wishList, String sns){

	    if(StringUtils.isBlank(sns)){
            return Result.error("请输入验证码");
        }

        String phone = wishList.getPhone();
        String rentType = wishList.getRentType();
        String houseType = wishList.getHouseType();
        String rentalPrice = wishList.getRentalPrice();
        String remark = wishList.getRemark();

        if(!Validator.isMobile(phone)){
            return Result.error("手机号格式不匹配，请输入正确的手机号！");
        }

        String codeKey = Constant.SNS__PREFIX__CODE + phone;
        try {
            //String snsRedis= RedisUtil.getString(codeKey);
            String snsRedis= (String) redisTemplate.opsForValue().get(codeKey);
            if(!StringUtils.equals(sns,snsRedis)){
                return Result.error("验证码错误");
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            logger.info("从Redis中读取验证码出错");
            return Result.error("请重新获取验证码后再试");
        }


        if(StringUtils.isBlank(rentType) || StringUtils.isBlank(houseType)
                || StringUtils.isBlank(rentalPrice) || StringUtils.isBlank(phone)){
            return Result.error("请完善心愿单信息，再提交！");
        }

        if((!StringUtils.equals("0",rentType)) && (!StringUtils.equals("1",rentType))){
            return Result.error("出租方式不匹配！");
        }

        if((!StringUtils.equals("1",houseType)) && (!StringUtils.equals("2",houseType))
                && (!StringUtils.equals("3",houseType)) && (!StringUtils.equals("4",houseType))){
            return Result.error("户型不匹配！");
        }

        if((!StringUtils.equals("0",rentalPrice)) && (!StringUtils.equals("1",rentalPrice))
                && (!StringUtils.equals("2",rentalPrice)) && (!StringUtils.equals("3",rentalPrice))
                && (!StringUtils.equals("4",rentalPrice)) && (!StringUtils.equals("5",rentalPrice))){
            return Result.error("租金范围不匹配！");
        }

        wishList.setRemark(StringUtils.isBlank(remark) ? "" : remark);

        try {
            wishListService.save(wishList);
        } catch (Exception e) {
            return Result.error("创建心愿单失败，请重试！");
        }

        return Result.ok();
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("wishlist:info")
	public Result info(@PathVariable("id") String id){
		WishListEntity wishList = wishListService.queryObject(id);
		
		return Result.ok().put("wishList", wishList);
	}
	

	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("wishlist:update")
	public Result update(@RequestBody WishListEntity wishList){
		wishListService.update(wishList);
		
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("wishlist:delete")
	public Result delete(@RequestBody String[] ids){
		wishListService.deleteBatch(ids);
		
		return Result.ok();
	}
	
}
